﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Linq.Mapping;
using Microsoft.Data.Sqlite;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using KorbeRTR.Utility;

namespace KorbeRTR.Models
{
    /// <summary>
    /// 温度记录
    /// </summary>
    public class TemperatureRecord
    {
        [Column]
        public Int64 Id { get; set; }

        /// <summary>
        /// 时间
        /// </summary>
        [Column]
        public DateTime Time { get; set; }

        /// <summary>
        /// 软删除标志
        /// </summary>
        [Column]
        public bool IsDeleted { get; set; }

        [Column(Name = "T0")]
        public double 除油温度 { get; set; }

        [Column(Name = "T1")]
        public double 热水洗温度 { get; set; }

        [Column(Name = "T2")]
        public double 整流机冷却 { get; set; }

        [Column(Name = "T3")]
        public double 酸洗槽 { get; set; }

        [Column(Name = "T4")]
        public double 镀铜 { get; set; }

        [Column(Name = "T5")]
        public double 微蚀 { get; set; }

        [Column(Name = "T6")]
        public double 热水槽 { get; set; }

        [Column(Name = "T7")]
        public double 烘干加热 { get; set; }

        [Column(Name = "T8")]
        public double 酸浸 { get; set; }

        [Column(Name = "T9")]
        public double 抗氧化 { get; set; }


        /// <summary>
        /// 删除n个月前的数据
        /// </summary>
        public static bool DeleteBeforeMonth(DateTime time, int n)
        {
            time = time.AddMonths(-n);
            time = new DateTime(time.Year, time.Month, 1, 0, 0, 0);

            int rows = OperationRecordDBHelper.ExecuteNonQuery("DELETE FROM TemperatureRecords WHERE Time < @Time",
                new SqliteParameter("@Time", time.ToSqlTime())
                );
            return rows > 0;
        }

        /// <summary>
        /// 添加温度记录
        /// </summary>
        /// <returns></returns>
        public static bool Add(TemperatureRecord record)
        {
            int rows = TemperatureDBHelper.ExecuteNonQuery(
                @"INSERT INTO TemperatureRecords(Time,T0,T1,T2,T3,T4,T5,T6,T7,T8,T9)
                            VALUES(@Time,@T0,@T1,@T2,@T3,@T4,@T5,@T6,@T7,@T8,@T9)",
                new SqliteParameter("@Time", record.Time.ToSqlTime()),
                new SqliteParameter("@T0", record.除油温度),
                new SqliteParameter("@T1", record.热水洗温度),
                new SqliteParameter("@T2", record.整流机冷却),
                new SqliteParameter("@T3", record.酸洗槽),
                new SqliteParameter("@T4", record.镀铜),
                new SqliteParameter("@T5", record.微蚀),
                new SqliteParameter("@T6", record.热水槽),
                new SqliteParameter("@T7", record.烘干加热),
                new SqliteParameter("@T8", record.酸浸),
                new SqliteParameter("@T9", record.抗氧化)
                );
            return rows > 0;
        }

        /// <summary>
        /// 列出温度记录
        /// </summary>
        /// <returns></returns>
        public static List<TemperatureRecord> ListAll()
        {
            var dt = TemperatureDBHelper.ExecuteTable("SELECT * FROM TemperatureRecords WHERE IsDeleted=FALSE");

            return dt.ToModel<TemperatureRecord>();
        }

        /// <summary>
        /// 查询 t0 到 t1 时间内的记录
        /// </summary>
        /// <param name="t0">起始时间</param>
        /// <param name="t1">终止时间</param>
        /// <returns></returns>
        public static List<TemperatureRecord> Select(DateTime t0, DateTime t1)
        {
            if (t0 > t1)
            {
                AuxLib.Swap(ref t0, ref t1);
            }

            var dt = TemperatureDBHelper.ExecuteTable("SELECT * FROM TemperatureRecords WHERE IsDeleted=FALSE AND (Time BETWEEN @T0 AND @T1)",
                new SqliteParameter("@T0", t0.ToSqlTime()),
                new SqliteParameter("@T1", t1.ToSqlTime())
                );

            return dt.ToModel<TemperatureRecord>();
        }

        /// <summary>
        /// 查询N天数据
        /// </summary>
        /// <param name="start">起始日期</param>
        /// <param name="days">天数</param>
        /// <returns></returns>
        public static List<TemperatureRecord> Select(DateTime aday, int days)
        {
            var t0 = aday.Date;
            var t1 = aday.Date.AddDays(days);
            return Select(t0, t1);
        }

        /// <summary>
        /// 查询某天的数据
        /// </summary>
        /// <param name="day"></param>
        /// <returns></returns>
        public static List<TemperatureRecord> Select(DateTime day)
        {
            return Select(day, 1);
        }
    }
}
